/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.attence;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attence.Duty;

/**
 * @author Administrator (email:raulcsj@163.com)
 * @since 2013-01-31
 */
@Service("dutyService")
public class DutyServiceImpl implements DutyService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Duty
     ****************************************/
    public void insert(Duty duty) throws Exception {
        duty.setDutyid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(duty);
    }

    /****************************************
     * Update a instace of Duty
     ****************************************/
    public void update(Duty duty) throws Exception {
        iDataSet4iBatis.update(duty);
    }

    /****************************************
     * Delete a instace of Duty
     ****************************************/
    public void delete(Duty duty) throws Exception {
        iDataSet4iBatis.delete(duty);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Duty duty) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Duty duty) throws Exception {
        String statementId = duty.getStatmentId();
        String mainId = duty.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> dutyList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Duty model = new Duty();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            dutyList.add(model);
        }
        iDataSet4iBatis.batchDelete(dutyList);
    }

    /****************************************
     * Query instaces of Duty
     ****************************************/
    public List<BaseModel> queryAsList(Duty duty) throws Exception {
        String queryListStatement = "";
        if (duty.isPaging()) {
            queryListStatement = duty.getStatmentId();
            duty.setStatmentId("Duty.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(duty);
            duty.setTotalCount(baseModel.getTotalCount());
            duty.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(duty);
    }

    /****************************************
     * Query a instaces of Duty
     ****************************************/
    public BaseModel queryAsObject(Duty duty) throws Exception {
        return iDataSet4iBatis.queryAsObject(duty);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
